import Mock from 'mockjs'
import querystring from 'querystring'

// 获取评论
Mock.mock(/\/api\/comment(\?.+)?/, 'get', options => {
    const params = querystring.parse(options.url.split('?')[1]),
        limit = params.limit || 10
    return Mock.mock({
        code: 0,
        msg: "",
        data: {
            total: 36,
            [`rows|${limit}`]: [
                {
                    id: '@guid',
                    nickname: "@cname",
                    content: "@cparagraph(1,4)",
                    blog: {
                        'id|1-5000': 1,
                        title: "@ctitle"
                    },
                    createDate: '@date("T")',
                    "avatar|1": [
                        "https://img0.baidu.com/it/u=791575638,2263885870&fm=26&fmt=auto",
                        "https://img0.baidu.com/it/u=3766229345,2700145838&fm=26&fmt=auto",
                        "https://img1.baidu.com/it/u=3508905900,472816329&fm=26&fmt=auto",
                        "https://img1.baidu.com/it/u=1640898348,2925693403&fm=26&fmt=auto",
                    ]
                }
            ]
        }
    })
})

// 提交评论
Mock.mock('/api/comment', 'post', data=>{
    const params = JSON.parse(data.body)
    return Mock.mock({
        code: 0,
        msg: "评论成功！",
        data: {
            id: '@guid',
            nickname: params.nickname,
            content: params.content,
            blog: {
                id: params.blog_id,
                title: "@ctitle"
            },
            createDate: +new Date(),
            "avatar|1": [
                "https://img2.baidu.com/it/u=4154275160,646411469&fm=26&fmt=auto",
                "https://img0.baidu.com/it/u=1111613319,3717565934&fm=26&fmt=auto",
                "https://img2.baidu.com/it/u=977441576,1069545220&fm=26&fmt=auto",
                "https://img2.baidu.com/it/u=2523062689,325181455&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500",
            ]
        }
    })
})